//
//  OrderItemDetailViewController.m
//  FreightAssistant
//
//  Created by leo on 15/12/12.
//  Copyright © 2015年 leo. All rights reserved.
//

#import "OrderItemDetailViewController.h"

#import "KKSupplyOrderDetailViewModel.h"
#import "KKPurchaseOrderDetailViewModel.h"

@interface OrderItemDetailViewController () {
    
    KKSupplyOrderDetailViewModel *_supplyOrderViewModel;
    KKPurchaseOrderDetailViewModel *_purchaseOrderViewModel;
    
    NSArray *_itemStateButtons;
}

@end

@implementation OrderItemDetailViewController

- (void)loadView {
    [super loadView];
    
    UIView *footerView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 50)];
    
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 6, CGRectGetWidth(self.view.bounds), 44)];
    [toolbar setBackgroundColor: [UIColor whiteColor]];
    [toolbar setBarTintColor: [UIColor whiteColor]];
    [toolbar setTranslucent: false];
    [footerView addSubview: toolbar];
    
    {
        UIBarButtonItem *item0 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil];
        
        UIImage *buttonImage = [[UIImage imageNamed: @"toolbar_background"] resizableImageWithCapInsets: UIEdgeInsetsMake(10, 10, 10, 10)];
        
        UIFont *buttonFont = [UIFont systemFontOfSize: 14];
        
        UIButton *button1 = [UIButton buttonWithType: UIButtonTypeCustom];
        button1.frame = CGRectMake(0, 0, 60, 30);
        button1.titleLabel.font = buttonFont;
        button1.tag = 1;
        [button1 setTitle: @"已支付" forState: UIControlStateNormal];
        [button1 setBackgroundImage: buttonImage forState: UIControlStateNormal];
        [button1 addTarget: self action: @selector(actionOfEditState:) forControlEvents: UIControlEventTouchUpInside];
        UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView: button1];
        
        UIButton *button2 = [UIButton buttonWithType: UIButtonTypeCustom];
        button2.frame = CGRectMake(0, 0, 60, 30);
        button2.titleLabel.font = buttonFont;
        button2.tag = 2;
        [button2 setTitle: @"已发货" forState: UIControlStateNormal];
        [button2 setBackgroundImage: buttonImage forState: UIControlStateNormal];
        [button2 addTarget: self action: @selector(actionOfEditState:) forControlEvents: UIControlEventTouchUpInside];
        UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithCustomView: button2];
        
        UIButton *button3 = [UIButton buttonWithType: UIButtonTypeCustom];
        button3.frame = CGRectMake(0, 0, 60, 30);
        button3.titleLabel.font = buttonFont;
        button3.tag = 3;
        [button3 setTitle: @"已收货" forState: UIControlStateNormal];
        [button3 setBackgroundImage: buttonImage forState: UIControlStateNormal];
        [button3 addTarget: self action: @selector(actionOfEditState:) forControlEvents: UIControlEventTouchUpInside];
        UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithCustomView: button3];
        
        UIButton *button4 = [UIButton buttonWithType: UIButtonTypeCustom];
        button4.frame = CGRectMake(0, 0, 60, 30);
        button4.titleLabel.font = buttonFont;
        button4.tag = 4;
        [button4 setTitle: @"已完成" forState: UIControlStateNormal];
        [button4 setBackgroundImage: buttonImage forState: UIControlStateNormal];
        [button4 addTarget: self action: @selector(actionOfEditState:) forControlEvents: UIControlEventTouchUpInside];
        UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithCustomView: button4];
        
        [toolbar setItems: @[item0,item1,item2,item3,item4]];
        
        _itemStateButtons = @[button1, button2, button3, button4];
    }
    
    [self.tableView setTableFooterView: footerView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"订单详情";
    
    if ([_kk_orderItem isKindOfClass: [KKPurchaseOrderItem class]]) {
        _purchaseOrderViewModel = [[KKPurchaseOrderDetailViewModel alloc] initWithModel: _kk_orderItem tableView: self.tableView];
    }
    else {
        _supplyOrderViewModel = [[KKSupplyOrderDetailViewModel alloc] initWithModel: _kk_orderItem tableView: self.tableView];
    }
    
    self.tableView.backgroundColor = [UIColor colorWithWhite:0.950 alpha:1.000];
    
    [self updateOrderStateUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private methods

- (void)updateOrderStateUI {
    
    NSInteger state = _kk_orderItem.orderItemState;
    for (NSInteger index = 0; index < _itemStateButtons.count; index++) {
        UIButton *itembutton = _itemStateButtons[index];
        itembutton.enabled = itembutton.tag <= state ? false : true;
    }
}

#pragma mark - Action methods

- (void)actionOfEditState:(UIButton *)button {
    
    NSInteger index = button.tag;
    
    __weak typeof(self) weakSelf = self;
    __weak typeof(_kk_orderItem) weakOrderItem = _kk_orderItem;
    
    if ([_kk_orderItem isKindOfClass: [KKPurchaseOrderItem class]]) {
        [KKDataBaseAPIInterface updatePurchaseOrderStateWithIdentifier: _kk_orderItem.kk_identifier state: index successful:^(id object) {
            [weakOrderItem setKk_state: @(index)];
            [weakSelf updateOrderStateUI];
            
        } faile:^(NSString *error) {
            [[KKResultAlertView sharedInstance] alertViewShowMessage: error andWithSuccessful: false andWithCallBack: nil];
        }];
    }
    else {
        [KKDataBaseAPIInterface updateSupplyOrderStateWithIdentifier: _kk_orderItem.kk_identifier state: button.tag successful:^(id object) {
            [weakOrderItem setKk_state: @(index)];
            [weakSelf updateOrderStateUI];
            
        } faile:^(NSString *error) {
            [[KKResultAlertView sharedInstance] alertViewShowMessage: error andWithSuccessful: false andWithCallBack: nil];
        }];
    }
}

@end
